package demop1;


import java.util.Arrays;

public class MyArrayList implements IList {
    public int[] array;
    public int uesdSize;
    public static final int DEFAULT_CAPACITY=10;
    public MyArrayList(){
        this.array=new int[DEFAULT_CAPACITY];
    }
    public void display(){
        for (int i = 0; i < this.uesdSize; i++) {
            System.out.print(array[i]+" ");
        }
    }

    @Override
    public void add(int data) {
        if (isFull()){
            grow();
        }
        this.array[uesdSize]=data;
        uesdSize++;
    }
    private boolean isFull(){
        return this.uesdSize==this.array.length;
    }
    private void grow(){
        this.array= Arrays.copyOf(this.array,2*array.length);
    }

    @Override
    public void add(int pos, int data) throws PosIllegal,EmptyException {
        try {
            //应该考虑pos位置的合法性和数组为空的可能性
            checkEmpty();
            checkPos(pos);
            if (isFull()){
                grow();
            }
            /*for (int i = pos; i <uesdSize-1 ; i++) {
                this.array[i+1]=this.array[i];
            }错误!!!!!!!!!!!!!!!!*/
            for (int i = this.uesdSize-1; i >=pos ; i--) {
                this.array[i+1]=this.array[i];
            }
            this.array[pos]=data;
            uesdSize++;
        }catch (PosIllegal e){
            e.printStackTrace();
            System.out.println("pos位置不合理");
        }catch (EmptyException e){
            e.printStackTrace();
            System.out.println("数组为空");
        }
    }
    private void checkPos(int pos){
        //if(pos<0||pos>=this.uesdSize)错误，因为是可以在最后一位插入的
        if(pos<0||pos>this.uesdSize){
            throw new PosIllegal("pos位置不合理（在checkPos方法中）");
        }
    }
    private void checkEmpty(){
        if (this.uesdSize==0){
            throw new EmptyException("数组为空（在checkEmpyt方法中）");
        }
    }

    @Override
    public boolean contains(int toFind) {
        /*for (int i = 0; i < this.uesdSize; i++) {
            return this.array[i]==toFind;
        }错误*/
        for (int i = 0; i < this.uesdSize; i++) {
            if(this.array[i]==toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int data) {
        for (int i = 0; i < this.uesdSize; i++) {
            if(array[i]==data){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) throws PosIllegal,EmptyException{
        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
        return -1;
    }
    private void checkPos2(int pos){
        if(pos<0||pos>=this.uesdSize){
            throw new PosIllegal("pos2位置不合法（在checkPos2方法中不合理）");
        }
    }

    @Override
    public void set(int pos, int data) {
        try{
            //注意：还要检查数组是否为空
            checkEmpty();
            checkPos2(pos);
            this.array[pos]=data;
        }catch (PosIllegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public void remove(int data) {
        for (int i = 0; i < this.uesdSize; i++) {
            if(array[i]==data){
                int pos=i;
                for (int j =pos; j < this.uesdSize; j++) {
                    array[j]=array[j+1];
                }
                uesdSize--;
            }
        }

    }

    @Override
    public int size() {
        return this.uesdSize;
    }

    @Override
    public void clear() {
        this.uesdSize=0;
    }
}
